<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style>
        .el-table .warning-row {
            background: oldlace;
        }

        .el-table .success-row {
            background: #f0f9eb;
        }
    </style>

</head>
<body>
<div id="app">


    <!--搜索表单-->

    <el-form :inline="true" :model="searchMap" class="demo-form-inline" label-width="80px" size="mini">

        <el-form-item label="类型">
            <el-select v-model="searchMap.ctype" placeholder="类型">
                <el-option label="主菜单" value="0"></el-option>
                <el-option label="二级菜单" value="1"></el-option>
                <el-option label="按钮" value="2"></el-option>
            </el-select>
        </el-form-item>

        <el-form-item label="模块名">
            <el-input v-model="searchMap.name" placeholder="请输入名称"></el-input>
        </el-form-item>

        <el-form-item>
            <el-button type="primary" @click="search">查询</el-button>
        </el-form-item>

        <el-button type="primary" style="float: right" size="small"
                   @click="dialogVisible = true;moduleData={};queryAllModule()">新增</el-button>
    </el-form>



    <!--添加数据对话框表单-->
    <el-dialog
            title="编辑模块"
            :visible.sync="dialogVisible"
            width="70%"
    >


        <el-form :model="moduleData" label-width="80px" size="mini">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="模块名" >
                            <el-input v-model="moduleData.name" autocomplete="off"></el-input>
                        </el-form-item>
                    </el-col>

                    <el-col :span="12">
                        <el-form-item label="上级模块" >
                            <el-select v-model="moduleData.parentId" placeholder="请选择上级模块">
                                <el-option v-for="module in modulelist"
                                           :label="module.name"
                                           :value="module.moduleId"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>

                </el-row>

                <el-row>
                    <el-col :span="12">
                        <el-form-item label="类型" >
                            <el-radio-group v-model="moduleData.ctype">
                                <el-radio label="0" >主菜单</el-radio>
                                <el-radio label="1" >二级菜单</el-radio>
                                <el-radio label="2" >按钮</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>

                    <el-col :span="12">
                        <el-form-item label="状态" >
                            <el-radio-group v-model="moduleData.state">
                                <el-radio label="1" >启用</el-radio>
                                <el-radio label="0" >停用</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row>
                    <el-col :span="12">
                        <el-form-item label="链接" >
                            <el-input v-model="moduleData.curl" autocomplete="off"></el-input>
                        </el-form-item>
                    </el-col>

                    <el-col :span="12">
                        <el-form-item label="备注" >
                            <el-input v-model="moduleData.remark" autocomplete="off"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>



            </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="addModule">确 定</el-button>
        </div>
    </el-dialog>


    <!--表格-->
    <template>
        <el-table
                :data="tableData"
                style="width: 100%"
                :row-class-name="tableRowClassName"
                @selection-change="handleSelectionChange"
        >

            <el-table-column
                    prop="name"
                    label="模块名"
                    align="center"
            >
            </el-table-column>
            <el-table-column
                    prop="ctype"
                    label="类型"
                    align="center"
            >
            </el-table-column>

            <el-table-column
                    prop="parentName"
                    align="center"
                    label="上级模块">
            </el-table-column>

            <el-table-column
                    prop="curl"
                    align="center"
                    label="链接">
            </el-table-column>

            <el-table-column
                    prop="state"
                    align="center"
                    label="状态">
            </el-table-column>

            <el-table-column
                    align="center"
                    label="操作">

                <template slot-scope="scope">
                    <el-row>
                        <el-button type="primary" size="mini" @click="editModule(scope.row)">修改</el-button>
                        <el-button type="danger" size="mini" @click="delModule(scope.row)">删除</el-button>
                    </el-row>
                </template>

            </el-table-column>

        </el-table>
    </template>

    <!--分页工具条-->
    <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="page.currentPage"
            :page-sizes="[5, 10, 20, 40]"
            :page-size="page.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page.total">
    </el-pagination>

</div>

<script src="../../js/vue.js"></script>
<script src="../../element-ui/lib/index.js"></script>
<link rel="stylesheet" href="../../element-ui/lib/theme-chalk/index.css">
<script src="../../js/axios-0.18.0.js"></script>

<script>
    new Vue({
        el: "#app",
        methods: {
            //删除模块
            delModule(row){
                this.$confirm('您确定删除该模块吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //点击确定 ，可以发送ajax请求进行删除
                    axios({
                        method:"delete",
                        url:"http://localhost:9000/module/"+row.moduleId
                    }).then((res)=>{
                        console.log(res.data);
                        if(res.data.flag){//删除成功
                            this.$message({
                                type: 'success',
                                message: res.data.message
                            });
                        }else {//删除失败
                            this.$message.error(res.data.message);;
                        }
                    }).catch((error)=>{

                    });

                }).catch(() => {
                    //点击取消
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });


            },
            //查询所有模块
            queryAll(){
                axios({
                    method:"get",
                    url:"http://localhost:9000/module/listall"
                }).then((res)=>{
                    console.log(res.data);
                    this.deptlist= res.data;
                }).catch((error)=>{

                });
            },
            //条件查询模块
            queryModule(){
                let reqData = {
                    "currentPage":this.page.currentPage,
                    "pageSize":this.page.pageSize,
                    "name":this.searchMap.name,
                    "ctype":this.searchMap.ctype
                };
                axios({
                    method:"post",
                    url:"http://localhost:9000/module/list",
                    data:reqData
                }).then((res)=>{
                    console.log(res.data);
                    this.tableData= res.data.rows;
                    this.page.total = res.data.total;
                }).catch((error)=>{

                });

            },
            tableRowClassName({row, rowIndex}) {
                if (rowIndex === 1) {
                    return 'warning-row';
                } else if (rowIndex === 3) {
                    return 'success-row';
                }
                return '';
            },
            // 复选框选中后执行的方法
            handleSelectionChange(val) {
                this.multipleSelection = val;
                console.log(this.multipleSelection)
            },
            // 查询方法
            search() {
                console.log(this.searchMap);
                this.page.currentPage=1;
                this.queryModule();
            },
            // 添加数据
            addModule(){
                console.log(this.moduleData);
                this.dialogVisible = false;
                let url="";
				let methodType="";
                if(this.moduleData.moduleId){
                    url = "http://localhost:9000/module/update";
					methodType="put";
                }else {
                    url = "http://localhost:9000/module/add";
					methodType="post";
                }
                axios({
                    method:methodType,
                    url:url, // /module/1001
                    data:this.moduleData,
                }).then((res)=>{
                    console.log(res.data);
                    if(res.data.flag){//成功
                        this.$message({
                            type: 'success',
                            message: res.data.message
                        });
                    }else {//失败
                        this.$message.error(res.data.message);;
                    }
                }).catch((error)=>{

                });
            },

            //分页
            handleSizeChange(val) {
                this.page.pageSize = val;
                this.queryModule();
            },
            handleCurrentChange(val) {
                this.page.currentPage = val;
                this.queryModule();
            },
            //根据id查询部门
            editModule(row){
                this.dialogVisible = true;
                axios({
                    method:"get",
                    url:"http://localhost:9000/module/" + row.moduleId
                }).then((res)=>{
                    console.log(res.data);
                    this.module= res.data;
                });
            },
            //查询所有模块
            queryAllModule() {
                this.dialogVisible = true;
                axios({
                    method:"get",
                    url:"http://localhost:9000/module/listall"
                }).then((res)=>{
                    console.log(res.data);
                    this.modulelist= res.data;
                })
            }

        },
        data() {
            return {
                page:{
                    pageSize:10,
                    total:100,
                    currentPage:1
                },
                // 添加数据对话框是否展示的标记
                dialogVisible: false,
                searchMap:{
                },
                // 模块模型数据
                moduleData: {

                },
                // 复选框选中数据集合
                multipleSelection: [],
                // 表格数据
                tableData: [],
                // 模块列表
                modulelist:[]
            }
        },
        mounted(){
            this.queryModule();
            this.queryAll();
        }
    })

</script>

</body>
</html>
